package com.ui.controller;

import com.ui.service.Impl.AttributeServiceImpl;
import com.ui.service.Impl.ProductKuidServiceImpl;
import com.ui.service.Impl.ProductServiceImpl;
import com.ui.service.ProductService;
import com.wanmei.exception.WanmeiException;
import com.wanmei.pojo.Attribute;
import com.wanmei.pojo.Product;
import com.wanmei.pojo.ProductStoreNum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class WebUiProductInfoController {
    @Autowired
    private ProductServiceImpl productService;
    @Autowired
    private AttributeServiceImpl attributeService;
    @Autowired
    private ProductKuidServiceImpl productKuidService;
    /**
     * 商品详情
     * @return
     */
    @RequestMapping("productInfoView")
    public ModelAndView productDetailView(@RequestParam("pid")Integer pid) {
        Product product = productService.selectByPrimaryKey(pid);
        System.out.println("商品：" + product);
        ModelAndView model = new ModelAndView();
        model.addObject("product",product);
        model.setViewName("productInfo");
        return model;
    }
    /**
     * 点击产品信息的时候，跳出产品的界面
     * @param pid
     * @return
     * @throws WanmeiException
     */
    @RequestMapping("/showProductInfo")
    public ModelAndView showProductInfo(@RequestParam("pid") int pid, HttpServletRequest req) throws WanmeiException {
        ModelAndView mv = new ModelAndView();
        //产品信息
        Product product = productService.selectByPrimaryKey(pid);
        mv.addObject("product", product);
//        默认配送信息
        //型号
        Attribute attribute = attributeService.getAttributeAllByCid(pid);
        mv.addObject("attribute",attribute);
        //库存
        List<ProductStoreNum> listProductNum = productKuidService.getProductStoreNum(pid);
        mv.addObject("listProductNum",listProductNum);

        for (ProductStoreNum p:listProductNum){
            System.out.println(p);
        }
        mv.setViewName("productInfo");
        return mv;
    }

    /**
     * 从当前型号或者款式和产品ID拿到库存
     * @param pid
     * @param vid
     * @return
     */
    @RequestMapping("/getStoreNum")
    @ResponseBody
    public Integer getStoreNum(@RequestParam("pid")int pid,@RequestParam("vid") int vid){
        return productKuidService.getStoreNumBySize(pid, vid);
    }



}
